//
//  RankingViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "RankingViewController.h"
#import "TenGrayShadowView.h"
#import "CommonStudyLinkHeaderView.h"
#import "StudyRankingTableViewCell.h"
#import "StudyRankingPersonInformationViewController.h"
#import "LpRankingPersonModel.h"
@interface RankingViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;

/**  数据  */
@property (strong, nonatomic) NSArray  *dataSouce;
/**  第一名数据  */
@property (strong, nonatomic) LpRankingPersonModel  *firstModel;
/**  我的数据  */
@property (strong, nonatomic) LpRankingPersonModel  *myModel;
/**  header  */
@property (strong, nonatomic) CommonStudyLinkHeaderView  *header;

@end

@implementation RankingViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDataSource];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"学习";

    [self layoutUI];
}

#pragma mark - 数据
- (void)getDataSource{
    __weak typeof(self) weakSelf = self;
    [[LPShowNoDataManager showNoDataManager] removeEmptyView];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"ACCOUNTID"] = ZJUSERINFORMODEL.ORG_ACCOUNT.ID;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFriendRanking] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataSouce = [NSArray yy_modelArrayWithClass:[LpRankingPersonModel class] json:object[@"ORD"]];
            for (LpRankingPersonModel *model  in weakSelf.dataSouce) {
                if ([model.ORDERBY isEqualToString:@"1"]) {
                    weakSelf.header.model = model;
                }
                if ([model.ACCOUNTID isEqualToString:ZJUSERINFORMODEL.ORG_ACCOUNT.ID]) {
                    weakSelf.myModel = model;
                }
            }
            [weakSelf.tableView reloadData];
            
        }
    } Fail:^(id object) {
        [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
        [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
            [weakSelf getDataSource];
        };
        
    }];
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    CommonStudyLinkHeaderView *header = [[CommonStudyLinkHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 220 * [UIScreen zj_screenHScale])];
    header.isName = NO;
    self.tableView.tableHeaderView = header;
    self.header = header;
    
}


#pragma mark - 私有方法
- (void)action_leftButton:(UIButton *)sender{
    [[ToolClass getMainNavigationController] popViewControllerAnimated:YES ];
}

#pragma mark - 监听事件


#pragma mark - 协议代理

/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.myModel != nil) {
            return 1;
        }else{
            return 0;
        }
    }else {
        return self.dataSouce.count;
    }
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    StudyRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[StudyRankingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    
    cell.section = indexPath.section;
    cell.row = indexPath.row;
    if (indexPath.section == 0) {
        cell.model = self.myModel;
    }else{
        cell.model = self.dataSouce[indexPath.row];
    }
    
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        
        LpRankingPersonModel *model = self.dataSouce[indexPath.row];
        if (![model.ACCOUNTID isEqualToString:ZJUSERINFORMODEL.ORG_ACCOUNT.ID]) {
            StudyRankingPersonInformationViewController *vc = [[StudyRankingPersonInformationViewController alloc] init];
            vc.personModel = model;
            vc.myModel = self.myModel;
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
        }
       
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    TenGrayShadowView *ten = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    
    return ten;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10 * [UIScreen zj_screenHScale];
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.bounds.size.width, self.view.bounds.size.height - navHeight - navHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}



@end
